package oj;

public class LC130 {
    //DFS递归
    public void solve(char[][] board) {
        int m=board.length;
        int n=board[0].length;
        if(m==0 && n==0) return;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                //从边界开始判断，与边界直接或间接相连的都递归修改成‘#’，修改完后再遍历一次
                boolean flag=i==0 || j==0 || i==m-1 || j==n-1;
                if(flag && board[i][j]=='O'){
                    dfs(board,i,j);
                }

            }
        }
        //如果board[i][j]=='#'说明是边界或与边界相连的'O'，否则就是需要修改的'O'
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(board[i][j]=='O'){
                    board[i][j]='X';
                }else if(board[i][j]=='#'){
                    board[i][j]='O';
                }
            }
        }
    }
    private void dfs(char[][]board,int i,int j){
        int m=board.length;
        int n=board[0].length;
        if(i<0 || j<0 || i>=m || j>=n || board[i][j]=='X' || board[i][j]=='#'){
            return;
        }
        board[i][j]='#';
        dfs(board,i+1,j);
        dfs(board,i,j+1);
        dfs(board,i-1,j);
        dfs(board,i,j-1);
    }
}
